/**
 * 插入排序
 *
 * @param {number[]} nums
 * @return {number[]}
 */
function insertSort(nums) {
  const length = nums.length;
  if (length < 2) {
    return nums;
  }
  let index;
  let current;
  for (let i = 1; i < length; i++) {
    index = i - 1;
    current = nums[i];
    while (index >= 0 && nums[index] > current) {
      nums[index + 1] = nums[index];
      index--;
    }
    nums[index + 1] = current;
  }
  return nums;
}

const nums = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
console.log(insertSort(nums));
